//
//  CinemaViewController.m
//  豆瓣
//
//  Created by dllo on 15/11/23.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "CinemaViewController.h"
#import "CinemaTableViewCell.h"
#import "Cinema.h"

@interface CinemaViewController () <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) NSMutableArray *cinemaArr;
@property (nonatomic, retain) UITableView *tableV;

@end

@implementation CinemaViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
  /********************************** 活动列表界面 *************************************/
    
    
    self.navigationItem.title = @"影院";
    
    [self getData];
    
    self.tableV = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 120) style: UITableViewStylePlain];
    self.tableV.delegate = self;
    self.tableV.dataSource = self;
    
    // 去掉cell间的分割线
    self.tableV.separatorStyle = NO;

    [self.view addSubview:self.tableV];
    [self.tableV release];
   
}

- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    while (self.cinemaArr.count == 0) {
        [self.tableV reloadData];
    }
}

// 获取并解析数据
- (void)getData {
    
    self.cinemaArr = [NSMutableArray array];
    
    NSString *urlStr = @"http://project.lanou3g.com/teacher/yihuiyun/lanouproject/cinemalist.php";
    
    NSString *urlEncode = [urlStr stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    NSURL *url = [NSURL URLWithString:urlEncode];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
    
    NSURLSessionDataTask *getTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
        
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSArray *arr = [[dic objectForKey:@"result"] objectForKey:@"data"];
        
        for (NSDictionary *tempDic in arr) {
            
            Cinema *cinema = [[Cinema alloc] init];
            [cinema setValuesForKeysWithDictionary:tempDic];
            cinema.cinemaId = [tempDic objectForKey:@"id"];
            
            [self.cinemaArr addObject:cinema];
            [cinema release];
            
        }
        
        [self.tableV reloadData];
    }];
    
    [getTask resume];
}

// 行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return [self.cinemaArr count];
}

// 行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 180;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *cellStr = @"cell";
    
    CinemaTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellStr];
    
    if (nil == cell) {
        
        cell = [[CinemaTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellStr];
    }
    
    Cinema *cinema = [self.cinemaArr objectAtIndex:indexPath.row];
    
    cell.nameLabel.text =cinema.cinemaName;
    cell.addressLabel.text = cinema.address;
    cell.phoneLabel.text = cinema.telephone;
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    return cell;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
  
}


@end
